'use strict';
const {userServe,Token}=require("user-serve")()
let user=new userServe
let token=new Token()
exports.main = (event, context) => {
	return new Promise((resolve, reject)=>{
		let r=function(data){
			console.log("执行成功: ",data);
			resolve(data)
		},j=function(err){
			console.log("执行失败: ",err);
			reject(err)
		}
		const {action,data}=event
		let returnInfo={//全局返回对象
			code:404,
			msg:"无"
		}
		switch(action){
			case "login":
				user.login(event.data).then(res=>{
					let tokenStr=token.set({username:data.username,data:Date.now()})
					if(res.code===0){
						r(Object.assign(res,{token:tokenStr}))
					}else{
						r(res)
					}
				}).catch(err=>{
					j(err)
				})
			break;
			case "getToken":
				console.log("action,data: ",action,data);
			break;
			case "setToken":
				console.log("action,data: ",action,data);
			break;
			case "getInfo":
				console.log("action,data: ",action,data);
			break;
			case "setAvatar":
				console.log("action,data: ",action,data);
			break;
			case "reg":
			user.reg(event.data).then(res=>{
				console.log("resaction: ",res);
				r(res)
			}).catch(err=>{
				j(err)
			})
			break;
			default:
				console.log("无对应命令:",action,data);
				//{username:data.username,data:Date.now()}
				console.log("token.gMDc1OTM2NCwiaWF0I): ",token.get())
			break;
		}
	})
	/**
	 返回代码设置
	 code     	  msg          		data         errMsg
	 
	 0          可选提示信息     看操作返回内容    
	 
	 400        错误信息
	 
	 404        内容不存在
	 
	 500        服务器错误 
				数据库操作错误等					错误信息
				
				
	 
	 */
	return event
};
